package start;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import start.temp.BFFoTask;
import start.temp.GSCloudResponse;
import start.util.FastJsonUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class FastJsonParse {

    public static void main(String[] args) {

        String filePath = "D:\\test.txt"; // 注意Windows路径使用双反斜杠
        String content = "";

        // 方法1：使用Files.readAllBytes（Java 7+，简洁方式）
        try {
            byte[] bytes = Files.readAllBytes(Paths.get(filePath));
            content = new String(bytes, StandardCharsets.UTF_8); // 指定字符集避免乱码
            System.out.println("文件内容读取成功，长度：" + content.length());
        } catch (IOException e) {
            System.err.println("读取失败：" + e.getMessage());
            e.printStackTrace();
        }


        TypeReference<GSCloudResponse<BFFoTask>> typeReference = new TypeReference<GSCloudResponse<BFFoTask>>() {};
        GSCloudResponse<BFFoTask> response = JSON.parseObject(content,typeReference);
        JSONObject jsonObject = JSON.parseObject(content);


        MaJiClass<Person> maJiClass = new MaJiClass();
        maJiClass.setClassName("111");
        maJiClass.getDatas().add(new Person("张三", 10L));
        maJiClass.getDatas().add(new Person("李四", 10L));

        MaJiClass<Person> maJiClass02 = new MaJiClass();
        maJiClass02.setClassName("111");
        maJiClass02.getDatas().add(new Person("张三", 20L));
        maJiClass02.getDatas().add(new Person("李四", 20L));

        List<MaJiClass<Person>> list = new ArrayList<>();
        list.add(maJiClass);
        list.add(maJiClass02);


        String s = FastJsonUtil.toJson(maJiClass);
        MaJiClass<Person> maJiClassAfter =  FastJsonUtil.fromJson(s, MaJiClass.class, Person.class);
        MaJiClass<Map> maJiClassMap =  FastJsonUtil.fromJson(s, MaJiClass.class, Map.class);


        String s01 = FastJsonUtil.toJson(list);
        List<MaJiClass<Person>> listMaJiClassAfter =  (List<MaJiClass<Person>>) FastJsonUtil.fromJsonToList(s01, MaJiClass.class, Person.class);


    }

}


class Person {
    private String name;
    private Long age;

    public Person(String name, Long age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getAge() {
        return age;
    }

    public void setAge(Long age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return name + " (" + age + ")";
    }
}

class MaJiClass<T> {
    private String className;

    private List<T> datas = new ArrayList<>();


    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public List<T> getDatas() {
        return datas;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
    }
}


